#include "bsp.h"
#include "dwt_delay.h"

#define VCOM_1_644 0x79
#define SPI_DELAY 5


extern UART_HandleTypeDef huart1;

/*int fputc(int ch, FILE *f)
{
	f = f; // unused warning prevention
	uint8_t str[1] = {ch};
	//uartOutCh( ch ) ;  // your character output function.
	HAL_UART_Transmit(&huart1, str, 1, 999);

	return ch;
}*/

void bsp_selfTest(void)
{
	uint8_t i, j;

	printf("bsp_selfTest\r\n");

	i = 8;
	while (i--)
	{
		HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
		HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
		DWT_Delay(300);
	}

	while (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_SET)
		;

	j = 8;
	while (j--)
	{
		HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
		HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
		DWT_Delay(300);
	}

	printf("bsp_selfTest\r\n");
}

void bsp_waitKey(void)
{
	while (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_SET)
		;
	return;
}

void bsp_led1(uint8_t status)
{
	if (status == 0)
	{
		HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
	}
	else
	{
		HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
	}

	return;
}

void bsp_led2(uint8_t status)
{
	if (status == 0)
	{
		HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
	}
	else
	{
		HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
	}

	return;
}

void bsp_ledPass(void){
	bsp_led1(0);
	
	bsp_led1(1);
	HAL_Delay(10);
	bsp_led1(0);
	HAL_Delay(30);
	
	bsp_led1(1);
	HAL_Delay(10);
	bsp_led1(0);
	HAL_Delay(50);
}

void bsp_ledFail(void){
	bsp_led1(0);
	
	while(1){
		bsp_led1(1);
		HAL_Delay(100);
		bsp_led1(0);
		HAL_Delay(100);
	}
	
}

/*
void lcd_spi_clk(void) {
	HAL_GPIO_WritePin(LCD_SCL_GPIO_Port, LCD_SCL_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(LCD_SCL_GPIO_Port, LCD_SCL_Pin, GPIO_PIN_SET);
}  */

#define LCD_CS_1() HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET);
#define LCD_CS_0() HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET);
#define LCD_SCL_1() HAL_GPIO_WritePin(LCD_SCL_GPIO_Port, LCD_SCL_Pin, GPIO_PIN_SET);
#define LCD_SCL_0() HAL_GPIO_WritePin(LCD_SCL_GPIO_Port, LCD_SCL_Pin, GPIO_PIN_RESET);
#define LCD_SDA_1() HAL_GPIO_WritePin(LCD_SDA_GPIO_Port, LCD_SDA_Pin, GPIO_PIN_SET);
#define LCD_SDA_0() HAL_GPIO_WritePin(LCD_SDA_GPIO_Port, LCD_SDA_Pin, GPIO_PIN_RESET);

#define RELAY_1() HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, GPIO_PIN_SET);
#define RELAY_0() HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, GPIO_PIN_RESET);

#define LCD_ATREN_1() HAL_GPIO_WritePin(LCD_ATREN_GPIO_Port, LCD_ATREN_Pin, GPIO_PIN_SET);
#define LCD_ATREN_0() HAL_GPIO_WritePin(LCD_ATREN_GPIO_Port, LCD_ATREN_Pin, GPIO_PIN_RESET);

void sda_input(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	HAL_GPIO_WritePin(GPIOA, LCD_SDA_Pin, GPIO_PIN_RESET);

	GPIO_InitStruct.Pin = LCD_SDA_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void sda_output(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	HAL_GPIO_WritePin(GPIOA, LCD_SDA_Pin, GPIO_PIN_RESET);

	GPIO_InitStruct.Pin = LCD_SDA_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

uint8_t READ_com(uint8_t Register)
{

	uint8_t x, data;
	data = 0;
	LCD_CS_1();
	DWT_Delay(SPI_DELAY);
	LCD_CS_0(); //CSB LOW
	DWT_Delay(SPI_DELAY);

	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_1(); //High = REad
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);

	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0(); //SID =0
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);
	//Write 6Bit Register Adresse
	for (x = 0; x < 6; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Register & 0x20) == 0x20)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Register = Register << 1;

		DWT_Delay(SPI_DELAY);
	}
	//Change SDA to input
	sda_input();
	//bcm2835_gpio_set_pud(sda, BCM2835_GPIO_PUD_UP);
	DWT_Delay(20000);
	//Read 8 Bit Register Data
	for (x = 0; x < 8; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		data = data << 1;
		//data=data+(bcm2835_gpio_lev(sda)&0x01);
		data = data + (HAL_GPIO_ReadPin(LCD_SDA_GPIO_Port, LCD_SDA_Pin));

		DWT_Delay(SPI_DELAY);
		DWT_Delay(SPI_DELAY);
	}

	LCD_CS_1(); //Reading complete

	//bcm2835_gpio_fsel(sda, BCM2835_GPIO_FSEL_OUTP);
	sda_output();
	LCD_SDA_0();
	DWT_Delay(SPI_DELAY);
	return data; //Return read out value
}

void Write_com(uint8_t Register, uint8_t Data)
{

	uint8_t x/*, regi,returnval, dat*/;
	//regi = Register;
	//dat = Data;
	LCD_CS_1();
	DWT_Delay(SPI_DELAY);
	LCD_CS_0(); //CSB LOW
	DWT_Delay(SPI_DELAY);

	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0(); //LOW = Write
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);
	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0(); //SID = 0
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);
	//Write 6Bit Register Adresse
	for (x = 0; x < 6; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Register & 0x20) == 0x20)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Register = Register << 1;
		DWT_Delay(SPI_DELAY);
	}
	//Write value into Register
	for (x = 0; x < 8; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Data & 0x80) == 0x80)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Data = Data << 1;

		DWT_Delay(SPI_DELAY);
	}

	LCD_CS_1();					//CSB high
	
}


void Write_com_check(uint8_t Register, uint8_t Data)
{

	uint8_t x, regi, returnval, dat;
	regi = Register;
	dat = Data;
	LCD_CS_1();
	DWT_Delay(SPI_DELAY);
	LCD_CS_0(); //CSB LOW
	DWT_Delay(SPI_DELAY);

	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0(); //LOW = Write
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);
	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0(); //SID = 0
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);
	//Write 6Bit Register Adresse
	for (x = 0; x < 6; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Register & 0x20) == 0x20)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Register = Register << 1;
		DWT_Delay(SPI_DELAY);
	}
	//Write value into Register
	for (x = 0; x < 8; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Data & 0x80) == 0x80)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Data = Data << 1;

		DWT_Delay(SPI_DELAY);
	}

	LCD_CS_1();					//CSB high
	
	
	returnval = READ_com(regi); //Check written data by reading the same Register out
	if (returnval == dat)
	{
		printf("Register 0x%02X ok \r\n", regi);
		bsp_ledPass();
	}
	else
	{
		printf("Register 0x%02X false \r\n", regi); //Abort if Data was not written corretly
		bsp_ledFail();									  //bcm2835_close();
		return;
	}
}

void Check_com(uint8_t Register, uint8_t Data)
{

	uint8_t /*x, */regi, returnval, dat;
	regi = Register;
	dat = Data;
	
	returnval = READ_com(regi); //Check written data by reading the same Register out
	printf("Register 0x%02X should be: 0x%02X, infact: 0x%02X \r\n", regi, Data, returnval);
	if (returnval == dat)
	{
		//printf("Register 0x%02X ok \r\n", regi);
		printf(" OK \r\n");
		bsp_ledPass();
	}
	else
	{
		//printf("Register 0x%02X false \r\n", regi); //Abort if Data was not written corretly
		printf(" FAILED \r\n");
		bsp_ledFail();									  //bcm2835_close();
		return;
	}
}


void setotp(uint8_t Register, uint8_t Data)
{
	uint8_t x;
	LCD_CS_1();
	DWT_Delay(SPI_DELAY);
	LCD_CS_0();
	DWT_Delay(SPI_DELAY);

	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0();
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);
	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0();
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);

	for (x = 0; x < 6; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Register & 0x20) == 0x20)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Register = Register << 1;

		DWT_Delay(SPI_DELAY);
	}

	for (x = 0; x < 8; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Data & 0x80) == 0x80)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Data = Data << 1;

		DWT_Delay(SPI_DELAY);
	}

	LCD_CS_1();
}

void writeotp(uint8_t Register, uint8_t Data)
{
	uint8_t x;

	LCD_CS_1();
	DWT_Delay(SPI_DELAY);
	LCD_CS_0();
	DWT_Delay(SPI_DELAY);

	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0();
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);
	LCD_SCL_0();
	DWT_Delay(SPI_DELAY);
	LCD_SDA_0();
	DWT_Delay(SPI_DELAY);
	LCD_SCL_1();
	DWT_Delay(SPI_DELAY);

	for (x = 0; x < 6; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Register & 0x20) == 0x20)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Register = Register << 1;

		DWT_Delay(SPI_DELAY);
	}

	for (x = 0; x < 8; x++)
	{
		LCD_SCL_0();
		DWT_Delay(SPI_DELAY);

		if ((Data & 0x80) == 0x80)
		{
			LCD_SDA_1();
		}
		else
		{
			LCD_SDA_0();
		}
		DWT_Delay(SPI_DELAY);

		LCD_SCL_1();
		Data = Data << 1;

		DWT_Delay(SPI_DELAY);
	}

	LCD_CS_1();

	DWT_Delay(21000);
}
void init(void)
{
	Write_com_check(0x00, 0x00); //PAGE 0

	//PFM Settings
	Write_com_check(0x0D, 0x07);
	Write_com_check(0x18, 0x79);
	Write_com_check(0x19, 0x95);
	Write_com_check(0x1A, 0x79);
	Write_com_check(0x1B, 0x95);
	Write_com_check(0x1C, 0x79);
	Write_com_check(0x1D, 0x95);
	Write_com_check(0x2A, 0x79);
	Write_com_check(0x2B, 0x95);

	Write_com_check(0x15, 0xDD); //AVDDP/AVDDN = 6.3V/-6.3V
	Write_com_check(0x16, 0xCD); //VGH/VGL=16V/-12V
	Write_com_check(0x17, 0xEE); //VSDP/VSDN=6.0V/-6.0V
	Write_com_check(0x20, VCOM_1_644); //VCOM
	Write_com_check(0x22, 0x30); //VGMPHS=5.6V
	Write_com_check(0x23, 0x02); //VGMPLS=0.4V
	Write_com_check(0x24, 0x10); //VGMNHS=-5.6V
	Write_com_check(0x25, 0x02); //VGMNLS=-0.4V

	Write_com_check(0x00, 0x01); //PAGE 1
	//Analog positive gamma reference

	Write_com_check(0x01, 0x00);
	Write_com_check(0x02, 0x01);
	Write_com_check(0x03, 0x0C);
	Write_com_check(0x04, 0x0A);
	Write_com_check(0x05, 0x34);
	Write_com_check(0x06, 0x09);
	Write_com_check(0x07, 0x13);
	Write_com_check(0x08, 0x1C);
	Write_com_check(0x09, 0x21);
	Write_com_check(0x0a, 0x24);
	Write_com_check(0x0b, 0x29);
	Write_com_check(0x0c, 0x2A);
	Write_com_check(0x0d, 0x29);
	Write_com_check(0x0e, 0x20);
	Write_com_check(0x0f, 0x12);
	Write_com_check(0x10, 0x36);
	Write_com_check(0x11, 0x0A);
	Write_com_check(0x12, 0x03);
	Write_com_check(0x13, 0x0F);

	//Analog negative gamma reference

	Write_com_check(0x14, 0x00);
	Write_com_check(0x15, 0x01);
	Write_com_check(0x16, 0x0C);
	Write_com_check(0x17, 0x0A);
	Write_com_check(0x18, 0x34);
	Write_com_check(0x19, 0x09);
	Write_com_check(0x1a, 0x13);
	Write_com_check(0x1b, 0x1C);
	Write_com_check(0x1c, 0x21);
	Write_com_check(0x1d, 0x24);
	Write_com_check(0x1e, 0x29);
	Write_com_check(0x1f, 0x2A);
	Write_com_check(0x20, 0x29);
	Write_com_check(0x21, 0x20);
	Write_com_check(0x22, 0x12);
	Write_com_check(0x23, 0x36);
	Write_com_check(0x24, 0x0A);
	Write_com_check(0x25, 0x03);
	Write_com_check(0x26, 0x0F);
	;
}

void init_191029(void)
{
	Write_com_check(0x00, 0x00); //PAGE 0

	//PFM Settings
	Write_com_check(0x0D, 0x07);
	Write_com_check(0x18, 0x79);
	Write_com_check(0x19, 0x95);
	Write_com_check(0x1A, 0x79);
	Write_com_check(0x1B, 0x95);
	Write_com_check(0x1C, 0x79);
	Write_com_check(0x1D, 0x95);
	Write_com_check(0x2A, 0x79);
	Write_com_check(0x2B, 0x95);

	Write_com_check(0x15, 0xDD); //AVDDP/AVDDN = 6.3V/-6.3V
	Write_com_check(0x16, 0xCD); //VGH/VGL=16V/-12V
	Write_com_check(0x17, 0xEE); //VSDP/VSDN=6.0V/-6.0V
	Write_com_check(0x20, VCOM_1_644); //VCOM
	Write_com_check(0x22, 0x30); //VGMPHS=5.6V
	Write_com_check(0x23, 0x02); //VGMPLS=0.4V
	Write_com_check(0x24, 0x10); //VGMNHS=-5.6V
	Write_com_check(0x25, 0x02); //VGMNLS=-0.4V

	Write_com_check(0x00, 0x01); //PAGE 1
	Write_com_check(0x00, 0x01);
	
	Write_com_check(0x01, 0x00);
	Write_com_check(0x02, 0x00);
	Write_com_check(0x03, 0x0B);
	Write_com_check(0x04, 0x07);
	Write_com_check(0x05, 0x2D);
	Write_com_check(0x06, 0x0A);
	Write_com_check(0x07, 0x14);
	Write_com_check(0x08, 0x1D);
	Write_com_check(0x09, 0x22);
	Write_com_check(0x0A, 0x25);
	Write_com_check(0x0B, 0x2A);
	Write_com_check(0x0C, 0x2A);
	Write_com_check(0x0D, 0x29);
	Write_com_check(0x0E, 0x1F);
	Write_com_check(0x0F, 0x10);
	Write_com_check(0x10, 0x2A);
	Write_com_check(0x11, 0x04);
	Write_com_check(0x12, 0x00);
	Write_com_check(0x13, 0x0F);
	Write_com_check(0x14, 0x00);
	Write_com_check(0x15, 0x00);
	Write_com_check(0x16, 0x0B);
	Write_com_check(0x17, 0x07);
	Write_com_check(0x18, 0x2D);
	Write_com_check(0x19, 0x0A);
	Write_com_check(0x1A, 0x14);
	Write_com_check(0x1B, 0x1D);
	Write_com_check(0x1C, 0x22);
	Write_com_check(0x1D, 0x25);
	Write_com_check(0x1E, 0x2A);
	Write_com_check(0x1F, 0x2A);
	Write_com_check(0x20, 0x29);
	Write_com_check(0x21, 0x1F);
	Write_com_check(0x22, 0x10);
	Write_com_check(0x23, 0x2A);
	Write_com_check(0x24, 0x04);
	Write_com_check(0x25, 0x00);
	Write_com_check(0x26, 0x0F);
	
}

void otp(void)
{
	printf("otp programm started\r\n");
	bsp_led1(1);
	bsp_led2(1);
	HAL_Delay(50);
	bsp_led1(0);
	bsp_led2(0);
	HAL_Delay(100);
	
	bsp_led1(1);
	bsp_led2(1);
	HAL_Delay(50);
	bsp_led1(0);
	bsp_led2(0);
	HAL_Delay(500);
	
	
	
	LCD_CS_1();
	LCD_SCL_1();
	LCD_SDA_0();
	LCD_ATREN_0(); //Disable OTP Auto refresh

	RELAY_0();
	DWT_Delay(20000);
	RELAY_1(); //SET OTPVDD to 7.5V
	DWT_Delay(20000);

	//init();
	init_191029();
	Write_com_check(0x00, 0x09); //PAGE 9
	Write_com_check(0x02, 0x66); //

	//write otp group 0x03h (page 0 Register 06h-0Dh)
	setotp(0x01, 0x03);
	writeotp(0x03, 0x01);

	//write otp group 0x05h	(page0 Register 14h-1Fh)
	Write_com_check(0x01, 0x05);
	writeotp(0x03, 0x01);

	//write otp group 0x06h	(page0 Register 20h)
	Write_com_check(0x01, 0x06);
	writeotp(0x03, 0x01);

	//write otp group 0x07h	(page 0 Register 22h-25h)
	Write_com_check(0x01, 0x07);
	writeotp(0x03, 0x01);

	//write otp group 0x1ah	(page 0 Register 28h-2Bh)
	Write_com_check(0x01, 0x1a);
	writeotp(0x03, 0x01);

	//write otp group 0x08h	(page 1 Register 01h-26h)
	Write_com_check(0x01, 0x08);
	writeotp(0x03, 0x01);

	RELAY_0();
	DWT_Delay(1000);

	Write_com_check(0x02, 0x00); //PAGE 0

	printf("otp programm finished");

	//bcm2835_close();

	bsp_led1(0);
	bsp_led2(1);
	return;
}

void readOtp(void)
{

	printf("read otp programm started\r\n");
	bsp_led1(1);
	bsp_led2(1);
	HAL_Delay(50);
	bsp_led1(0);
	bsp_led2(0);
	HAL_Delay(100);
	
	bsp_led1(1);
	bsp_led2(1);
	HAL_Delay(50);
	bsp_led1(0);
	bsp_led2(0);
	HAL_Delay(500);
	
	
	
	LCD_CS_1();
	LCD_SCL_1();
	LCD_SDA_0();
	//LCD_ATREN_0(); //Disable OTP Auto refresh
	LCD_ATREN_1(); //Disable OTP Auto refresh

	/*RELAY_1();
	DWT_Delay(20000);
	RELAY_0(); //SET OTPVDD to 7.5V
	DWT_Delay(20000);*/
	RELAY_0();
	//init();
	
	
	Write_com(0x00, 0x00); //PAGE 0

	//PFM Settings
	Check_com(0x0D, 0x07);
	Check_com(0x18, 0x79);
	Check_com(0x19, 0x95);
	Check_com(0x1A, 0x79);
	Check_com(0x1B, 0x95);
	Check_com(0x1C, 0x79);
	Check_com(0x1D, 0x95);
	Check_com(0x2A, 0x79);
	Check_com(0x2B, 0x95);

	Check_com(0x15, 0xDD); //AVDDP/AVDDN = 6.3V/-6.3V
	Check_com(0x16, 0xCD); //VGH/VGL=16V/-12V
	Check_com(0x17, 0xEE); //VSDP/VSDN=6.0V/-6.0V
	Check_com(0x20, VCOM_1_644); //VCOM
	Check_com(0x22, 0x30); //VGMPHS=5.6V
	Check_com(0x23, 0x02); //VGMPLS=0.4V
	Check_com(0x24, 0x10); //VGMNHS=-5.6V
	Check_com(0x25, 0x02); //VGMNLS=-0.4V

	Write_com(0x00, 0x01); //PAGE 1
	//Analog positive gamma reference

	Check_com(0x01, 0x00);
	Check_com(0x02, 0x01);
	Check_com(0x03, 0x0C);
	Check_com(0x04, 0x0A);
	Check_com(0x05, 0x34);
	Check_com(0x06, 0x09);
	Check_com(0x07, 0x13);
	Check_com(0x08, 0x1C);
	Check_com(0x09, 0x21);
	Check_com(0x0a, 0x24);
	Check_com(0x0b, 0x29);
	Check_com(0x0c, 0x2A);
	Check_com(0x0d, 0x29);
	Check_com(0x0e, 0x20);
	Check_com(0x0f, 0x12);
	Check_com(0x10, 0x36);
	Check_com(0x11, 0x0A);
	Check_com(0x12, 0x03);
	Check_com(0x13, 0x0F);

	//Analog negative gamma reference

	Check_com(0x14, 0x00);
	Check_com(0x15, 0x01);
	Check_com(0x16, 0x0C);
	Check_com(0x17, 0x0A);
	Check_com(0x18, 0x34);
	Check_com(0x19, 0x09);
	Check_com(0x1a, 0x13);
	Check_com(0x1b, 0x1C);
	Check_com(0x1c, 0x21);
	Check_com(0x1d, 0x24);
	Check_com(0x1e, 0x29);
	Check_com(0x1f, 0x2A);
	Check_com(0x20, 0x29);
	Check_com(0x21, 0x20);
	Check_com(0x22, 0x12);
	Check_com(0x23, 0x36);
	Check_com(0x24, 0x0A);
	Check_com(0x25, 0x03);
	Check_com(0x26, 0x0F);
	
	
	
	
	/*Write_com_check(0x00, 0x09); //PAGE 9
	Write_com_check(0x02, 0x66); //

	//write otp group 0x03h (page 0 Register 06h-0Dh)
	setotp(0x01, 0x03);
	writeotp(0x03, 0x01);

	//write otp group 0x05h	(page0 Register 14h-1Fh)
	Write_com_check(0x01, 0x05);
	writeotp(0x03, 0x01);

	//write otp group 0x06h	(page0 Register 20h)
	Write_com_check(0x01, 0x06);
	writeotp(0x03, 0x01);

	//write otp group 0x07h	(page 0 Register 22h-25h)
	Write_com_check(0x01, 0x07);
	writeotp(0x03, 0x01);

	//write otp group 0x1ah	(page 0 Register 28h-2Bh)
	Write_com_check(0x01, 0x1a);
	writeotp(0x03, 0x01);

	//write otp group 0x08h	(page 1 Register 01h-26h)
	Write_com_check(0x01, 0x08);
	writeotp(0x03, 0x01);

	RELAY_1();
	DWT_Delay(1000);

	Write_com_check(0x02, 0x00); //PAGE 0*/

	printf("read otp programm finished\r\n");

	//bcm2835_close();

	bsp_led1(0);
	bsp_led2(1);
	return;
}




	/*if (!bcm2835_init())
	return 1;
	//uint8_t i;
bcm2835_gpio_fsel(sda, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(cs, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(scl, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(ATREN, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(otpin, BCM2835_GPIO_FSEL_OUTP);  */

